import pymysql
from pyecharts.charts import Bar
from pyecharts import options as opts

# 数据库连接配置
db = pymysql.connect(
    host="localhost",
    port=3306,
    user="root",
    database="crawlerdemo",
    password="woshiliuhao0",
    charset="utf8"
)

cursor = db.cursor()
sql = "select job_name,count(*) from job GROUP BY job_name "

try:
    cursor.execute(sql)
    res = cursor.fetchall()
    print(res)
    # 从查询结果中提取数据
    job_places = [item[0] for item in res]  # 提取岗位职业
    job_counts = [item[1] for item in res]  # 提取岗位数量

    # 创建柱状图
    bar = Bar()
    bar.add_xaxis(job_places)  # 设置x轴数据
    bar.add_yaxis("岗位数量", job_counts)

    bar.set_global_opts(title_opts=opts.TitleOpts(title="岗位数量占比"))
    # 渲染到HTML文件中
    bar.render("piera.html")
except Exception as e:
    print(e)
    print("查询或图表创建出现问题")

